package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.system.entity.FileLogEntity;
import  com.sq.universe.system.service.FileLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@RestController
@RequestMapping("/fileLog")
@CrossOrigin
public class FileLogController extends BaseController {

    @Autowired
    private FileLogService fileLogService;

    /**
    * 查询数据
    * @param fileLogEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:fileLog:query")
    public RespEntity query(FileLogEntity fileLogEntity){
        try {
            return new RespEntity("查询成功",fileLogService.query(fileLogEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param fileLogEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:fileLog:queryByPage")
    public RespEntity queryByPage(FileLogEntity fileLogEntity){
        try {
            PageHelper.startPage(fileLogEntity.getPageNo(),fileLogEntity.getPageSize());
            List<FileLogEntity> fileLogList = fileLogService.query(fileLogEntity);
            PageInfo<FileLogEntity> pageInfo = new PageInfo<FileLogEntity>(fileLogList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param fileLogEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:fileLog:insert")
    public RespEntity insert(FileLogEntity fileLogEntity){
        try {
            fileLogEntity.setFlCreateTime(new Date());
            fileLogEntity.setFlCreateUser(this.getCurrentUser().getUserId());
            fileLogService.insert(fileLogEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param fileLogEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:fileLog:update")
    public RespEntity update(FileLogEntity fileLogEntity){
        try {
            fileLogEntity.setFlUpdateTime(new Date());
            fileLogEntity.setFlUpdateUser(this.getCurrentUser().getUserId());
            fileLogService.update(fileLogEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param fileLogEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:fileLog:delete")
    public RespEntity delete(FileLogEntity fileLogEntity){
        try {
            if(fileLogEntity.getFlId() == null){
                return new RespEntity("删除失败");
            }
            fileLogEntity.setFlUpdateTime(new Date());
            fileLogEntity.setFlUpdateUser(this.getCurrentUser().getUserId());
            fileLogService.logicalDelete(fileLogEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("删除失败");
        }
    }

    /**
    * 根据ID查询
    * @param fileLogEntity
    * @return
    */
    @PostMapping("/getById")
    @RequiresPermissions("system:fileLog:getById")
    public RespEntity getById(FileLogEntity fileLogEntity){
        try {
            if(fileLogEntity.getFlId() == null){
                return new RespEntity("请输入id查询");
            }
            return new RespEntity("查询成功",fileLogService.getById(fileLogEntity.getFlId()));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 根据实体查询
    * @param fileLogEntity
    * @return
    */
    @PostMapping("/get")
    @RequiresPermissions("system:fileLog:get")
    public RespEntity get(FileLogEntity fileLogEntity){
        try {
            return new RespEntity("查询成功",fileLogService.get(fileLogEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }
}